package web.gkw.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import web.gkw.dao.UserDAO;
import web.gkw.model.User;


@Component("userDAO")
public class UserDAOImpl implements UserDAO {
	
	final static String hql_login = "from user  where username=:username and password=:password";
	
	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		System.out.println(session.save(user));
	}

	@Override
	public User login(User user) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		Query query =  session.createQuery(hql_login);
		query.setString("username", user.getUsername());
		query.setString("password",user.getPassword());
		User res = (User) query.uniqueResult();
		return res;
	}

}
